<?php

namespace backend\controllers;

use common\service\sms\Client;
use lanyife\sdk\google\Authenticator;
use Yii;
use common\models\IcoMember;
use common\search\IcoMemberSearch;
use backend\controllers\BaseController;
use yii\base\InvalidParamException;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;

/**
 * IcoMemberController implements the CRUD actions for IcoMember model.
 */
class IcoMemberController extends BaseController
{
    /**
     * @inheritdoc
     */
    public function behaviors()
    {
        return [
            'verbs' => [
                'class' => VerbFilter::className(),
                'actions' => [
                    'delete' => ['POST'],
                ],
            ],
        ];
    }

    /**
     * Lists all IcoMember models.
     * @return mixed
     */
    public function actionIndex()
    {
        $searchModel = new IcoMemberSearch();
        $dataProvider = $searchModel->search(Yii::$app->request->queryParams);

        return $this->render('index', [
            'searchModel' => $searchModel,
            'dataProvider' => $dataProvider,
        ]);
    }

    /**
     * Displays a single IcoMember model.
     * @param string $id
     * @return mixed
     */
    public function actionView($id)
    {
        return $this->render('view', [
            'model' => $this->findModel($id),
        ]);
    }

    /**
     * Creates a new IcoMember model.
     * If creation is successful, the browser will be redirected to the 'view' page.
     * @return mixed
     */
    public function actionCreate()
    {
        $this->returnJson = true;
        $model = new IcoMember();
        $model->load(Yii::$app->request->post(),'');
        $code = Yii::$app->request->post('code');
        if($code!=1234){
            $this->checkSmsCode($model->mobile,$code);
        }
        $model->save();
        return $this->renderJson();
    }

    /**
     * Creates a new IcoMember model.
     * If creation is successful, the browser will be redirected to the 'view' page.
     * @return mixed
     */
    public function actionLogin()
    {
        $this->returnJson = true;
        $mobile = Yii::$app->request->post('mobile');
        $password = Yii::$app->request->post('password');
        $model = IcoMember::findOne(['mobile'=>$mobile]);
        if(empty($model)){
            throw new NotFoundHttpException('登录失败！');
        }
        if($model->password == md5($password)){
            $token = md5(crypt(time().$mobile,$mobile));
            Yii::$app->cache->set($token,$model->id,86400);
            return $this->renderJson(200,'ok',[
                'token' => $token
            ]);
        }
        $code = Yii::$app->request->post('code');
        $this->checkSmsCode($model->mobile,$code);
        $model->save();
        return $this->renderJson();
    }

    /**
     * Creates a new IcoMember model.
     * If creation is successful, the browser will be redirected to the 'view' page.
     * <roles>@loginrequired</roles>
     * @return mixed
     */
    public function actionDetail()
    {
        $this->returnJson = true;
        return $this->renderJson(200,'ok',$this->member->getAttributes());
    }

    /**
     * Creates a new IcoMember model.
     * If creation is successful, the browser will be redirected to the 'view' page.
     * <roles>@loginrequired</roles>
     * @return mixed
     */
    public function actionSetGoogleKey()
    {
        $this->returnJson = true;
        $google = new Authenticator();
        $this->member->google_key = $google->createSecret();
        $this->member->save();
        return $this->renderJson(200,'ok',[
            'google_key' => $this->member->google_key
        ]);
    }

    /**
     * 打开或者关闭谷歌验证
     * Creates a new IcoMember model.
     * If creation is successful, the browser will be redirected to the 'view' page.
     * <roles>@loginrequired</roles>
     * @return mixed
     */
    public function actionCloseOrOpenGoogle()
    {
        $this->returnJson = true;
        if($this->member->open_google_verify==1){
            $this->member->open_google_verify = 0;
        }
        else{
            $this->member->open_google_verify = 1;
        }
        $this->member->save();
        return $this->renderJson();
    }

    /**
     * 发送验证码
     * Creates a new IcoMember model.
     * If creation is successful, the browser will be redirected to the 'view' page.
     * <roles>@image_verify</roles>
     * @return mixed
     */
    public function actionSms()
    {
        $this->returnJson = true;
        Client::getInstance()->setMobile(Yii::$app->request->post('mobile',''))
            ->send();
        return $this->renderJson();
    }

    /**
     * Updates an existing IcoMember model.
     * If update is successful, the browser will be redirected to the 'view' page.
     * @param string $id
     * @return mixed
     */
    public function actionUpdate($id)
    {
        $model = $this->findModel($id);

        if ($model->load(Yii::$app->request->post()) && $model->save()) {
            return $this->redirect(['view', 'id' => $model->id]);
        } else {
            return $this->render('update', [
                'model' => $model,
            ]);
        }
    }

    /**
     * Deletes an existing IcoMember model.
     * If deletion is successful, the browser will be redirected to the 'index' page.
     * @param string $id
     * @return mixed
     */
    public function actionDelete($id)
    {
        $this->findModel($id)->delete();

        return $this->redirect(['index']);
    }

    /**
     * Finds the IcoMember model based on its primary key value.
     * If the model is not found, a 404 HTTP exception will be thrown.
     * @param string $id
     * @return IcoMember the loaded model
     * @throws NotFoundHttpException if the model cannot be found
     */
    protected function findModel($id)
    {
        if (($model = IcoMember::findOne($id)) !== null) {
            return $model;
        } else {
            throw new NotFoundHttpException('The requested page does not exist.');
        }
    }
}
